<template>
	<view class="container">
		<uni-forms ref="baseForm" :rules="rules" :modelValue="baseFormData" label-width="80px">
			<uni-forms-item label="腕带号" class="wounded" name="bh" required>
				<uni-easyinput :disabled="disabled" class="wounded-input" v-model="baseFormData.bh"
					placeholder="请输入腕带号" />
				<!-- <button class="wounded-button">腕带扫描</button>style="width: 150px;float: left;" -->
		
			</uni-forms-item>
			<uni-forms-item label="姓名" class="wounded" required name="xm">
				<uni-easyinput :disabled="disabled" v-model="baseFormData.xm" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="性别" class="wounded" required name="xb">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.xb" :localdata="sex" />
			</uni-forms-item>
			<uni-forms-item label="年龄" class="wounded">
				<uni-easyinput :disabled="disabled" v-model="baseFormData.nl" placeholder="请输入年龄" />
			</uni-forms-item>
			<uni-forms-item label="负伤时间" class="wounded" required name="fssj">
				<uni-datetime-picker :disabled="disabled" type="datetime"
					v-model="baseFormData.fssj" />
			</uni-forms-item>
			<uni-forms-item label="到达时间" class="wounded" required name="ddsj">
				<uni-datetime-picker :disabled="disabled" type="datetime"
					v-model="baseFormData.ddsj" />
			</uni-forms-item>
			<uni-forms-item label="负伤地点" class="wounded" required name="fsdd">
				<uni-easyinput :disabled="disabled" v-model="baseFormData.fsdd" placeholder="请输入负伤地点" />
			</uni-forms-item>
			<uni-forms-item label="部别" class="wounded">
				<uni-easyinput :disabled="disabled" v-model="baseFormData.bb" placeholder="请输入部别" />
			</uni-forms-item>
			<uni-forms-item label="职务" class="wounded">
				<uni-data-select :disabled="disabled" v-model="baseFormData.zw" :localdata="typeArrOffice"
					placeholder="请选择职务" />
			</uni-forms-item>
			<uni-forms-item label="军衔" class="wounded">
				<uni-data-select :disabled="disabled" v-model="baseFormData.jx" :localdata="typeArrMilitary"
					placeholder="请选择军衔" />
			</uni-forms-item>
			<uni-forms-item label="血型" class="wounded">
				<uni-data-select :disabled="disabled" v-model="baseFormData.xx" :localdata="typeArrBlood"
					placeholder="请选择血型" />
			</uni-forms-item>
			<uni-forms-item label="民族" class="wounded">
				<uni-data-select :disabled="disabled" v-model="baseFormData.mz" :localdata="typeArrEthnic"
					placeholder="请输入民族" />
			</uni-forms-item>
			<uni-forms-item label="家庭住址" class="wounded">
				<uni-easyinput :disabled="disabled" type="textarea" v-model="baseFormData.jtzz" placeholder="请输入家庭住址" />
			</uni-forms-item>
			<uni-forms-item label="伤部" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.sb" multiple :localdata="hobbysWounded" />
			</uni-forms-item>
			<uni-forms-item label="伤类" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.sl" multiple
					:localdata="hobbysInjuries" />
			</uni-forms-item>
			<uni-forms-item label="伤型" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.sx" multiple
					:localdata="hobbysInjuryType" />
			</uni-forms-item>
			<uni-forms-item label="并发症" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.bfz" multiple
					:localdata="hobbysComplication" />
			</uni-forms-item>
			<uni-forms-item label="伤势" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.ss" multiple
					:localdata="hobbysInjuryForce" />
			</uni-forms-item>
			<uni-forms-item label="伤票类型" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.splx" multiple
					:localdata="hobbysTicketType" />
			</uni-forms-item>
			<uni-forms-item label="负伤类型" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.fslx" multiple
					:localdata="hobbysTypeInjury" />
			</uni-forms-item>
			<uni-forms-item label="处置方式" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.czfs" multiple
					:localdata="hobbysDisposal" />
			</uni-forms-item>
			<uni-forms-item label="主诉" class="wounded">
				<uni-easyinput :disabled="disabled" type="textarea" v-model="baseFormData.zs" placeholder="请输入主诉" />
			</uni-forms-item>
			<uni-forms-item label="现病史" class="wounded">
				<uni-easyinput :disabled="disabled" type="textarea" v-model="baseFormData.xbs" placeholder="请输入现病史" />
			</uni-forms-item>
			<uni-forms-item label="摘要" class="wounded">
				<uni-easyinput :disabled="disabled" type="textarea" v-model="baseFormData.bz" placeholder="请输入摘要" />
			</uni-forms-item>
			<view class="fixedBottom">
				<button v-if="!disabled" class="woundedSave" @click="woundedSave('baseForm')">提交</button>
				<button v-if="!disabled" class="woundedList" @click="woundedList()">伤员列表</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import { getStorageSync,setStorageSync,removeStorageSync } from '../../utils/utils.js'
	export default {
		data() {
			return {
				rules: {
					bh: {
						rules: [{
							required: true,
							errorMessage: '请扫描腕带号'
						}],
					},
					xm: {
						rules: [{
							required: true,
							errorMessage: '请 输入名称'
						}],
					},
					xb: {
						rules: [{
							required: true,
							errorMessage: '请选择性别名称'
						}],
					},
					fssj: {
						rules: [{
							required: true,
							errorMessage: '请输入负伤时间'
						}],
					},
					fsdd: {
						rules: [{
							required: true,
							errorMessage: '请输入负伤地点'
						}],
					}
				},
				baseFormData: {
					bh: null,
					xm: null,
					xb: null,
					nl: null,
					fssj: null,
					ddsj: null,
					fsdd: null,
					bb: null,
					zw: null,
					jx: null,
					xx: null,
					mz: null,
					jtzz: null,
					sb: [],
					sl: [],
					sx: [],
					bfz: [],
					ss: [],
					splx: [],
					fslx: [],
					czfs: [],
					zs: null,
					xbs: null,
					bz: null
				},
				hobbysDisposal: [],
				hobbysTypeInjury: [],
				hobbysTicketType: [],
				hobbysInjuryForce: [],
				hobbysComplication: [],
				hobbysInjuryType: [],
				hobbysInjuries: [],
				hobbysWounded: [],
				typeArrOffice: [],
				typeArrMilitary: [],
				typeArrBlood: [],
				typeArrEthnic: [],
				sex: [{
						text: '男',
						value: '男'
					},
					{
						text: '女',
						value: '女'
					}
				],
				query:[],
				disabled: false,
				update1: false
			}
		},
		onLoad(option){
			if(Object.keys(option).length){
				if(JSON.parse(option.data).update===1){
					uni.setNavigationBarTitle({
						title: '修改伤员信息'
					})
					this.update1=true
					this.updateDetail(JSON.parse(option.data).bh)
				}else{
					uni.setNavigationBarTitle({
						title: '伤员信息详情'
					})
					this.detail(JSON.parse(option.data))
				}
			}
			this.hobbysDisposal=getStorageSync('hobbysDisposal')
			this.hobbysTypeInjury=getStorageSync('hobbysTypeInjury')
			this.hobbysTicketType=getStorageSync('hobbysTicketType')
			this.hobbysInjuryForce=getStorageSync('hobbysInjuryForce')
			this.hobbysComplication=getStorageSync('hobbysComplication')
			this.hobbysInjuryType=getStorageSync('hobbysInjuryType')
			this.hobbysInjuries=getStorageSync('hobbysInjuries')
			this.hobbysWounded=getStorageSync('hobbysWounded')
			this.typeArrOffice=getStorageSync('typeArrOffice')
			this.typeArrMilitary=getStorageSync('typeArrMilitary')
			this.typeArrBlood=getStorageSync('typeArrBlood')
			this.typeArrEthnic=getStorageSync('typeArrEthnic')
		},
		methods: {
			// 修改查询
			updateDetail(data){
				for (let key in data) {
					const arr = ['sb','sl','sx','bfz','ss','splx','fslx','czfs']
					this.baseFormData[key] = data[key]
					arr.forEach((item,index)=>{
						if(key===item){
							if(RegExp(/,/).test(data[key])){
								this.baseFormData[item] = data[item].split(',')
							}else{
								this.baseFormData[item] = [data[item]]
							}
						}
					})
				}
			},
			// 修改
			update(){
				var query=getStorageSync('baseFormData')
				query.forEach((item,index)=>{
					if(item.bh==this.baseFormData.bh){
						this.$set(query,index,{
							// 编号
							bh: this.baseFormData.bh,
							// 姓名
							xm: this.baseFormData.xm,
							// 性别
							xb: this.baseFormData.xb,
							// 年龄
							nl: this.baseFormData.nl,
							// 负伤时间
							fssj: this.baseFormData.fssj,
							// 到达时间
							ddsj: this.baseFormData.ddsj,
							// 负伤地点
							fsdd: this.baseFormData.fsdd,
							// 部别
							bb: this.baseFormData.bb,
							// 职务
							zw: this.baseFormData.zw,
							// 军衔
							jx: this.baseFormData.jx,
							// 血型
							xx: this.baseFormData.xx,
							// 民族
							mz: this.baseFormData.mz,
							// 家庭住址
							jtzz: this.baseFormData.jtzz,
							// 伤部
							sb: this.baseFormData.sb.join(','),
							// 伤类
							sl: this.baseFormData.sl.join(','),
							// 伤型
							sx: this.baseFormData.sx.join(','),
							// 并发症
							bfz: this.baseFormData.bfz.join(','),
							// 伤势
							ss: this.baseFormData.ss.join(','),
							//伤票类型
							splx: this.baseFormData.splx.join(','),
							// 负伤类型
							fslx: this.baseFormData.fslx.join(','),
							// 处置方式
							czfs: this.baseFormData.czfs.join(','),
							// 主诉
							zs: this.baseFormData.zs,
							// 现病史
							xbs: this.baseFormData.xbs,
							// 摘要
							bz: this.baseFormData.bz
						})
					}
				})
				setStorageSync('baseFormData',query)
			},
			// 详情
			detail(data){
				for (let key in data) {
					const arr = ['sb','sl','sx','bfz','ss','splx','fslx','czfs']
					this.baseFormData[key] = data[key]
					arr.forEach((item,index)=>{
						if(key===item){
							if(RegExp(/,/).test(data[key])){
								this.baseFormData[item] = data[item].split(',')
							}else{
								this.baseFormData[item] = [data[item]]
							}
						}
					})
				}
				this.disabled = true
			},
			woundedList(){
				uni.navigateTo({
					url:'/pages/offlineCasualtyList/index'
				})
			},
			woundedSave(ref){
				console.log(this.disabled);
				if(this.update1){
					this.update()
					uni.navigateTo({
						url:'/pages/casualtyList/index'
					})
				}else{
					// console.log(getStorageSync('baseFormData')=='');
					// console.log(getStorageSync('baseFormData'));
					this.$refs[ref].validate().then(res => {
						var baseData = {
							// 编号
							bh: this.baseFormData.bh,
							// 姓名
							xm: this.baseFormData.xm,
							// 性别
							xb: this.baseFormData.xb,
							// 年龄
							nl: this.baseFormData.nl,
							// 负伤时间
							fssj: this.baseFormData.fssj,
							// 到达时间
							ddsj: this.baseFormData.ddsj,
							// 负伤地点
							fsdd: this.baseFormData.fsdd,
							// 部别
							bb: this.baseFormData.bb,
							// 职务
							zw: this.baseFormData.zw,
							// 军衔
							jx: this.baseFormData.jx,
							// 血型
							xx: this.baseFormData.xx,
							// 民族
							mz: this.baseFormData.mz,
							// 家庭住址
							jtzz: this.baseFormData.jtzz,
							// 伤部
							sb: this.baseFormData.sb.join(','),
							// 伤类
							sl: this.baseFormData.sl.join(','),
							// 伤型
							sx: this.baseFormData.sx.join(','),
							// 并发症
							bfz: this.baseFormData.bfz.join(','),
							// 伤势
							ss: this.baseFormData.ss.join(','),
							//伤票类型
							splx: this.baseFormData.splx.join(','),
							// 负伤类型
							fslx: this.baseFormData.fslx.join(','),
							// 处置方式
							czfs: this.baseFormData.czfs.join(','),
							// 主诉
							zs: this.baseFormData.zs,
							// 现病史
							xbs: this.baseFormData.xbs,
							// 摘要
							bz: this.baseFormData.bz
						}
						if(getStorageSync('baseFormData')!=''){
							this.query=getStorageSync('baseFormData')
							this.query.push(baseData)
							setStorageSync('baseFormData',this.query)
						}else{
							this.query.push(baseData)
							setStorageSync('baseFormData',this.query)
						}
						this.query=[]
						this.baseFormData={}
						uni.navigateTo({
							url:'/pages/offlineCasualtyList/index'
						})
					}).catch(err => {
						console.log('err', err);
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	::v-deep.uni-section .uni-section-header__decoration {
		background-color: #23440d !important;
	}

	::v-deep.uni-data-checklist .checklist-group .checklist-box.is--default.is-checked .radio__inner {
		border-color: #23440d !important;
	}

	::v-deep.uni-data-checklist .checklist-group .checklist-box.is--default.is-checked .radio__inner .radio__inner-icon {
		background-color: #23440d !important;
	}

	::v-deep.uni-data-checklist .checklist-group .checklist-box.is--default.is-checked .checklist-text {
		color: #23440d;
	}

	::v-deep.uni-data-checklist .checklist-group .checklist-box.is--default.is-checked .checkbox__inner {
		border-color: #23440d;
		background-color: #23440d;
	}

	.container {
		padding-bottom: 200rpx;
	}

	/*腕带号输入框*/
	// .wounded-input{
	// 	width: 40%;
	// 	float: left;
	// }
	/*输入框样式*/
	.wounded {
		margin: 5%;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	/*腕带扫描样式*/
	.wounded-button {
		width: 100px;
		height: 70rpx;
		border-radius: 15px;
		font-size: 28rpx;
		line-height: 70rpx;
		color: #FFF;
		outline: none;
		background-color: #23440d;
		letter-spacing: 1px;
	}

	/*表单样式*/
	.wounded-name {
		width: 80%;
	}

	.fixedBottom {
		height: 200rpx;
		background-color: #FFF;
		position: fixed;
		bottom: 0rpx;
		left: 0;
		right: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 50;

		button {
			width: 300rpx;
			height: 88rpx;
			color: white;
			background-color: #23440d;
			border-radius: 20px;
			line-height: 88rpx;
			text-align: center;
		}
	}
</style>
